//
//  NSString+LKJSON.m
//  YuxiCloud
//
//  Created by LIUKUN-PC on 16/10/21.
//  Copyright © 2016年 ynnissi. All rights reserved.
//

#import "NSString+LKJSON.h"


@implementation NSString (LKJSON)

- (NSDictionary *)dictionaryJson {
    id data = [self objectJson];
    if ([data isKindOfClass:[NSDictionary class]]) {
        return data;
    }
    return nil;
}

- (NSArray *)arrayJson {
    id data = [self objectJson];
    if ([data isKindOfClass:[NSArray class]]) {
        return data;
    }
    return nil;
}

- (id)objectJson {
    
    NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id object = [NSJSONSerialization JSONObjectWithData:jsonData
                                              options:NSJSONReadingMutableContainers
                                                error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return object;
    
}

+ (NSString *)stringWithJsonObject:(id)object {
    
    if (![NSJSONSerialization isValidJSONObject:object]) {
        return nil;
    }
    
    NSError *parseError = nil;
    
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&parseError];
    
    if (parseError) {
        return nil;
    }
    
    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    
    return [str stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
    
}


@end


@implementation NSObject (LKJSON)


@end
